package com.anjubao.parking.municipal.basezone.service;

import com.anjubao.parking.municipal.basezone.dto.CitiesResDto;
import com.anjubao.parking.municipal.basezone.dto.DefaultCityResDto;
import com.anjubao.parking.municipal.basezone.dto.DistrictsResDto;
import com.anjubao.parking.municipal.basezone.dto.ProvincesResDto;
import com.anjubao.parking.municipal.basezone.mapper.BaseZoneMapper;
import com.anjubao.parking.municipal.basezone.model.BaseZone;
import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.common.utils.U;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BaseZoneService extends ServiceImpl<BaseZoneMapper,BaseZone>  {

    @Autowired
    private BaseZoneMapper baseZoneMapper;

    public List<ProvincesResDto> getProvinces(){
        return baseZoneMapper.findProvinces();
    }

    public List<CitiesResDto> getCities(int provinceId){
        return baseZoneMapper.findCities(provinceId);
    }

    public List<DistrictsResDto> getDistricts(int cityId){
        return baseZoneMapper.findDistricts(cityId);
    }

    @Transactional
    public DefaultCityResDto setDefaultCity(int cityId) {
        baseZoneMapper.updateDefaultCity();
        baseZoneMapper.updateDefaultCityByCityId(cityId);
        return getDefaultCity();
    }

    public DefaultCityResDto getDefaultCity() throws ServiceException {
        DefaultCityResDto defaultCityResDto=new DefaultCityResDto();
        defaultCityResDto.setCity(baseZoneMapper.findDefaultCity());
        defaultCityResDto.setProvinces(baseZoneMapper.findProvicesByDefaultCity());
        if (U.isEmpty(defaultCityResDto.getCity())) {
            throw new ServiceException("默认城市Id不存在!");
        }
        defaultCityResDto.setDistricts(baseZoneMapper.findDistricts(defaultCityResDto.getCity().getCityId()));
        return defaultCityResDto;
    }
}
